<?php
/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2022/5/25
 * Time: 15:39
 */

namespace App\Http\Controllers\Api;


use App\Model\AdminLog;
use App\Model\LuckDraw;
use App\Model\OperationLogs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class LuckDrawController extends BaseController
{

    protected $model = null;
    /**
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->model = new LuckDraw();
    }

    public function getUserStatistic(Request $request){
        return $this->toJson(self::SUCCESS, $this->model->getUserStatistic($request));
    }

    public function getType(){
        return $this->toJson(self::SUCCESS, $this->model->getType());
    }

    public function getLuckDraw(Request $request)
    {
        $params = $request->all();
        $where = [];

        $page = $params['page'] ?? 0;
        $limit = $params['limit'] ?? 10;
        if ($page<1){$page=1;}

        return $this->toJson(self::SUCCESS, $this->model->getLuckDraw($where, $page, $limit));
    }

    public function editLuckDraw(Request $request)
    {
        $params = $request->all();
        $res = $this->model->editLuckDraw($params);
        if ($res && $res['code']==0){
            AdminLog::createData($request, AdminLog::GOODS, '抽奖活动编辑');
            return $this->toJson(self::SUCCESS);
        }
        return $this->toJson(self::ERROR, [], $res['msg']??'异常');
    }



    public function getManualAward(Request $request){
        return $this->toJson(self::SUCCESS, $this->model->getManualAward($request));
    }

    public function addManualAward(Request $request){

        $validator = Validator::make($request->all(), [
            'luckId' => 'required',
            'user_id' => 'required',
            'awardId' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->toJson(self::PARAM_ERROR, [], $validator->errors()->all()[0]);
        }
        $data = $this->model->addManualAward($request->all());
        if ($data['code'] !== 0) {
            return $this->toJson(self::ERROR, $data);
        }

        OperationLogs::createData($request, OperationLogs::GOODS, '新增能量箱补偿');
        return $this->toJson(self::SUCCESS, $data);
    }

    public function delManualAward(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->toJson(self::PARAM_ERROR, [], $validator->errors()->all()[0]);
        }
        $data = $this->model->delManualAward($request->input('id'));
        if ($data['code'] !== 0) {
            return $this->toJson(self::ERROR, $data);
        }

        OperationLogs::createData($request, OperationLogs::GOODS, '新增能量箱补偿删除');
        return $this->toJson(self::SUCCESS, $data);
    }



    public function getActivityPool (Request $request){
        return $this->toJson(self::SUCCESS, $this->model->getActivityPool($request));
    }

    public function addActivityPool (Request $request){
        $desc = $request->input('desc');
        if (!$desc){
            return $this->toJson(self::ERROR, '参数缺失');
        }
        $data = $this->model->addActivityPool($desc);
        if ($data['code'] !== 0) {
            return $this->toJson(self::ERROR, $data);
        }
        OperationLogs::createData($request, OperationLogs::GOODS, '新增奖池');
        return $this->toJson(self::SUCCESS, $data);
    }

    public function delActivityPool (Request $request){
        $id = $request->input('id');
        if (!$id){
            return $this->toJson(self::ERROR, '参数缺失');
        }
        $data =  $this->model->delActivityPool($id);
        if ($data['code'] !== 0) {
            return $this->toJson(self::ERROR, $data);
        }
        OperationLogs::createData($request, OperationLogs::GOODS, '删除奖池');
        return $this->toJson(self::SUCCESS, $data);
    }

    public function setActivityPool (Request $request){
        $pools = $request->input('pools');
        if (empty($pools)) {
            return $this->toJson(self::ERROR, '参数缺失');
        }
        $data = $this->model->setActivityPool($pools);
        if ($data['code'] !== 0) {
            return $this->toJson(self::ERROR, $data);
        }
        OperationLogs::createData($request, OperationLogs::GOODS, '设置奖池');
        return $this->toJson(self::SUCCESS, $data);
    }

    public function upActivityPool (Request $request){
        $id = $request->input('id');
        $desc = $request->input('desc');
        if (!$id || !$desc){
            return $this->toJson(self::ERROR, '参数缺失');
        }
        $data = $this->model->upActivityPool($id,$desc);
        if ($data['code'] !== 0) {
            return $this->toJson(self::ERROR, $data);
        }
        OperationLogs::createData($request, OperationLogs::GOODS, '修改奖池', $id);
        return $this->toJson(self::SUCCESS, $data);
    }

    //许愿池抽奖统计
    public function awardStatisticsList(Request $request){
        return $this->toJson(self::SUCCESS, $this->model->awardStatisticsList($request));
    }

}
